package com.fick.struts.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts.actions.DispatchAction;

import com.fick.hibernate.dao.*;
import com.fick.hibernate.model.*;
import com.fick.tools.SystemLogger;
import com.fick.util.General;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ManageCandidateAction extends DispatchAction{
	
	private static final Log log = LogFactory.getLog(ManageCandidateAction.class);
	
	
	public ActionForward addCan(ActionMapping mapping,ActionForm form, HttpServletRequest request,
			HttpServletResponse response)
	{
		String jid = request.getParameter("jid");
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String age = request.getParameter("age");
		String ismarry = request.getParameter("ismarry");
		String school = request.getParameter("school");
		String academic = request.getParameter("academic");
		String speciality = request.getParameter("speciality");
		String offscholltime = request.getParameter("offscholltime");
		String tel = request.getParameter("tel");
		String email = request.getParameter("email");
		String resume = request.getParameter("resume");
		String capacity = request.getParameter("capacity");

		try{
			Job job = new Job();
			JobDAO jobDAO = new JobDAO();
			job = jobDAO.findById(Integer.valueOf(jid));
			
			SimpleDateFormat   sdf   =   new   SimpleDateFormat("yyyy-MM-dd");   
			Date date = new Date();
			if(offscholltime != null && !offscholltime.trim().equals(""))
			{
				date = sdf.parse(offscholltime);
			}  
			
			Candidate can = new Candidate();
			CandidateDAO canDAO = new CandidateDAO();
			
			can.setJob(job);
			can.setName(name);
			can.setSex(Integer.valueOf(sex));
			can.setBirth(Integer.valueOf(age));
			can.setIsmarry(Integer.valueOf(ismarry));
			can.setScholl(school);
			can.setAcademic(academic);
			can.setSpeciality(speciality);
			can.setOffscholltime(date);
			can.setTel(tel);
			can.setEmail(email);
			can.setResume(resume);
			can.setCapacity(capacity);
			can.setAddtime(new Date());
			
			canDAO.save(can);
			
			request.getSession().setAttribute("message","简历申请成功");
			response.sendRedirect("zw_list.jsp?typeid=4&bigclassid=14");
			
			return null;
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "应聘记录", "添加应聘记录失败");
			return null;
		}
	}
	public ActionForward delCan(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String cid = request.getParameter("cid");
		try{
			CandidateDAO canDAO = new CandidateDAO();
			Candidate candidate = new Candidate();
			candidate = canDAO.findById(Integer.valueOf(cid));
			
			canDAO.delete(candidate);
			request.setAttribute("message","删除应聘记录成功");
			return mapping.findForward("canList");
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "应聘记录", "删除应聘记录失败");
			return null;
		}
	}
	
	public ActionForward delCanByStr(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		SystemLogger.operatelog(request, "删除多个应聘记录");
		
		String str = request.getParameter("str");
		CandidateDAO canDAO = new CandidateDAO();
		Candidate candidate = new Candidate();
		String[] ids = str.split(",");
		int id = 0;
		try {
			for (int i = 0; i < ids.length; i++) {
				id = Integer.parseInt(ids[i]);
				candidate = canDAO.findById(id);
				canDAO.delete(candidate);
			}
			request.setAttribute("message","删除多条应聘记录成功");
			return mapping.findForward("canList");
		} catch (RuntimeException e) {
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "应聘记录", "删除应聘记录失败");
			return null;
		}
	}
}
